package Four;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * 服务器
 * @author 长风无言
 */
public class Recive {

    public static void main(String[] args) {

        ServerSocket ss = null;
        Socket s = null;
        ObjectInputStream in = null;
        ObjectOutputStream out = null;

        try {
            //创建服务端
            ss = new ServerSocket(8888);
            System.out.println("等待连接");
            //接收连接
            s = ss.accept();
            //关联上输入流
            in = new ObjectInputStream(s.getInputStream());
            //将接收的数据强制转换为UserMassege类型
            UserMessage userMessage = (UserMessage) in.readObject();
            //判断账号和密码是否正确
            if ("admin".equals(userMessage.getUser().getAccount()) && "123456".equals(userMessage.getUser().getPassword())) {
                //正确就修改类型为”success“
                userMessage.setCheckFlag("success");
                //错误改为fail
            } else {
                userMessage.setCheckFlag("fail");
            }
            System.out.println(userMessage.getCheckFlag());
            //关联上输出流
            out = new ObjectOutputStream(s.getOutputStream());
            //将修改后的信息发送回客户端
            out.writeObject(userMessage);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ss) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
